$(function () {
    $("#datagrid").datagrid({
        title: "考勤列表",
        fit: true,
        fitColumns: true,
        pagination: true,
        url: "/attendance/query.do",
        columns: [[
            {field: 'id', title: '考勤编号', width: 100},
            {
                field: 'employee', title: '姓名', width: 100, formatter: function (employee) {
                return employee ? employee.realname : "";
            }
            },
            {field: 'nowDate', title: '有效日期', width: 100},
            {
                field: 'signOn', title: '签到时间', width: 100
            },
            {field: 'signDowm', title: '签退时间', width: 100},
            {
                field: 'onState', title: '签到状态', width: 100, formatter: function (onState) {
                return onState == true ? "签到正常" : "<font style='color: red;'>未签到</font>"
            }
            },
            {
                field: 'downState', title: '签退状态', width: 100, formatter: function (downState) {
                return downState == true ? "签退正常" : "<font style='color: red;'>未签退</font>"
            }
            },
            {field: 'signUp', title: '补签时间', width: 100}
        ]],
        rownumbers: true,
        singleSelect: true,
        toolbar: "#toolbars"
    });

    $("#dialog").dialog({
        height: 266,
        width: 214,
        closed: true
    });
})

//签到时间样式
function formatterTime(signOn) {
    var dateString = 8;
    var date = new Date();
    var newDate = date.getHours();
    if (signOn != null) {
        if (newDate > dateString) {
            return "<font style='color: red;'>" + signOn + "</font>";
        } else {
            return signOn;
        }
    } else {
        return " ";
    }
}


//签到样式状态
function formatterOnState(onState) {
    var dateString = 8;
    var date = new Date();
    var newDate = date.getHours();
    if (onState == true) {
        if (newDate > dateString) {
            return "<font style='color: red;'>迟到了</font>";
        } else {
            return "签到正常";
        }
    } else {
        return "未签到";
    }
}

//签退样式状态
function formatterDownState(downState) {
    var dateString = 17;
    var date = new Date();
    var newDate = date.getHours();
    if (downState == true) {
        if (newDate < dateString) {
            return "<font style='color: red;'>早退了</font>";
        } else {
            return "签退正常";
        }
    } else {
        return "未签退";
    }
}


//签到
function onState() {
    $.get("/attendance/onState.do", function (data) {
        if (data.success) {
            $.messager.alert("温馨提示", "签到成功", "info", function () {
                $("#datagrid").datagrid("reload");
            });
        } else {
            $.messager.alert("温馨提示", data.msg, "info");
        }
    })
}

//签退
function downState() {
    $.get("/attendance/downState.do", function (data) {
        if (data.success) {
            $.messager.alert("温馨提示", "签退成功", "info", function () {
                $("#datagrid").datagrid("reload");
            });
        } else {
            $.messager.alert("温馨提示", data.msg, "info");
        }
    })
}

//补签
function signUp() {
    $("#dialog").dialog("open");
    //获取到选中的行的信息
    var row = $("#datagrid").datagrid("getSelected");
    //若当前选中了一行数据
    if (row) {
        $("#calendar").calendar({
            fit: true,
            fitColumns: true,
            onSelect: function (date) {//返回当前选择的时间
                $.get("/attendance/signUp.do", {selectDate: date, employeeId: row.employee.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", "补签成功!", "info", function () {
                            $("#datagrid").datagrid("load");
                            $("#datagrid").datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            $("#datagrid").datagrid("load");
                        });
                    }
                })
                $("#dialog").dialog("close", true);
            }
        })
    } else {
        $("#calendar").calendar({
            fit: true,
            fitColumns: true,
            onSelect: function (date) {//返回当前选择的时间
                $.get("/attendance/signUp.do", {selectDate: date}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", "补签成功!", "info", function () {
                            $("#datagrid").datagrid("load");
                            $("#datagrid").datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            $("#datagrid").datagrid("load");
                        });
                    }
                })
                $("#dialog").dialog("close", true);
            }
        })
    }
}

function reload() {
    $("#datagrid").datagrid("reload");
}

//高级查询
function queryObject() {
    var employeeName = $("#employeeName").textbox("getValue");
    var beginDate = $("#beginDate").datebox("getValue");
    var endDate = $("#endDate").datebox("getValue");
    $("#datagrid").datagrid("load", {
        employeeName: employeeName,
        beginDate: beginDate,
        endDate: endDate
    });
}